﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using System.Data.SqlClient;

namespace JSales.ModuleSystem
{
    public partial class dbsetForm : Form
    {
        private static bool IsCanConnectioned = false;
        
        
        public dbsetForm()
        {
            InitializeComponent();
        }
        #region config文件操作
        ///<summary> 
        ///返回＊.exe.config文件中appSettings配置节的value项  
        ///</summary> 
        ///<param name="strKey"></param> 
        ///<returns></returns> 
        public static string GetAppConfig(string strKey)
        {
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == strKey)
                {
                    return ConfigurationManager.AppSettings[strKey];
                }
            }
            return null;
        }

        ///<summary>  
        ///在＊.exe.config文件中appSettings配置节增加一对键、值对  
        ///</summary>  
        ///<param name="newKey"></param>  
        ///<param name="newValue"></param>  
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }

            // Open App.Config of executable  
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // You need to remove the old settings object before you can replace it  
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            // Add an Application Setting.  
            config.AppSettings.Settings.Add(newKey, newValue);
            // Save the changes in App.config file.  
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.  
            ConfigurationManager.RefreshSection("appSettings");
        }
        #endregion
        private void button2_Click(object sender, EventArgs e)
        {
            MySec sec = new MySec();
            UpdateAppConfig("dbServer",textBox1.Text);
            UpdateAppConfig("dbPort", textBox2.Text);
            UpdateAppConfig("dbName", textBox3.Text);
            UpdateAppConfig("dbUser", textBox4.Text);
            UpdateAppConfig("dbPswd", sec.Encrypto(textBox5.Text));

            this.Close();
        }

        private void dbsetForm_Load(object sender, EventArgs e)
        {
            //textBox1.Text = ConfigurationManager.AppSettings["dbServer"].ToString();
            //textBox2.Text = ConfigurationManager.AppSettings["dbPort"].ToString();
            //textBox3.Text = ConfigurationManager.AppSettings["dbName"].ToString();
            //textBox4.Text = ConfigurationManager.AppSettings["dbUser"].ToString();
            //textBox5.Text = ConfigurationManager.AppSettings["dbPswd"].ToString();
            textBox1.Text = GetAppConfig("dbServer");
            textBox2.Text = GetAppConfig("dbPort");
            textBox3.Text = GetAppConfig("dbName");
            textBox4.Text = GetAppConfig("dbUser");
            MySec sec = new MySec();
            textBox5.Text = sec.Decrypto(GetAppConfig("dbPswd"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySec sec = new MySec();
            string conn = "Data Source="
                          + ConfigurationManager.AppSettings["dbServer"].ToString()
                          +","
                          +ConfigurationManager.AppSettings["dbPort"].ToString()
                          + ";Initial Catalog="
                          +ConfigurationManager.AppSettings["dbName"].ToString()
                          +";Persist Security Info=True;User ID="
                          + ConfigurationManager.AppSettings["dbUser"].ToString()
                          +";Password="
                          + sec.Decrypto(ConfigurationManager.AppSettings["dbPswd"].ToString());
            //MessageBox.Show(conn);
            SqlConnection dbconection = new SqlConnection(conn);
            try
            {
                dbconection.Open();
                MessageBox.Show("数据库连接成功");
            }
            catch
            {
                MessageBox.Show("数据库连接失败");
            }
            finally
            {
                dbconection.Close();
            }
        }
    }
}
